MAIN=ITunesServer
VERSION=0.3
NAME=iwebapp
DIST=$(NAME)-$(VERSION)
MACHINE=$(shell uname)
SEP=;
if $(MACHINE) = Darwin then
	SEP=:
MAC_CLASSPATH=/System/Library/Java
GENFILES=Builds.java License.java
IPHONE="/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app/Contents/MacOS/iPhone Simulator"

all: .classpath
	javac -g -classpath `cat .classpath` *.java

.classpath: lib Makefile
	rm -f $@
	touch $@
	echo .$(SEP)$(MAC_CLASSPATH)$(SEP)`ls lib/*.jar | xargs | sed -e "s/ /$(SEP)/g"` > $@

%.class:%.java .classpath $(GENFILES)
	javac -g -classpath `cat .classpath`  $<

%.run:%.class .classpath
	java -classpath `cat .classpath` $*

%.tar.gz:%.tar
	gzip -f -9 $<

run: $(MAIN).run
r: allclean run
ac: allclean
ir: allclean ITunesServer.run
lw: limewire
lw: allclean Limewire.run
tt: TextTransfer.run
dist: $(NAME).tar.gz
	mkdir -p release
	cp $< release/$(DIST).tar.gz
	cp release/$(DIST).tar.gz ~/Desktop

mains:
	grep "extends LocalServer" *.java | sed s/\.java.*//

phone: Makefile
	$(IPHONE) &

jar: $(NAME).jar
open:
	open http://jeffpalm.com/webapp

keynote: KeynoteServer.run
itunes: ITunesServer.run
simple: Simple.run
demo: Demo.run
darth: Demo.run
google: Google.run
buttons: Buttons.run
remote: Remote.run
facebook: Facebook.run
nintendo: Nintendo.run
fb: facebook
speech: Remote.class
	java -classpath `cat .classpath` Remote etc/speech.ir
web:
	touch lastUpdated.inc
	rm lastUpdated.inc
	make lastUpdated.inc
limewire: Limewire.class

lastUpdated.inc: Makefile *.php doc/*.php
	echo `date` > $@

License.java: Makefile LICENSE
	rm -f $@
	touch $@
	echo "final class License {" >> $@
	echo "  static void print() {" >> $@
	cat LICENSE | sed -e 's/"/\\"/g' | sed -e 's/^/    System.err.println\("/g'  | sed -e 's/$$/"\);/g' >> $@
	echo "  }" >> $@
	echo "}" >> $@

Builds.java: Makefile
	rm -f $@
	touch $@
	echo "final class Builds {" >> $@
	echo "  final static String BUILD_DATE_STRING = \"" `date` "\".trim();" >> $@
	echo "  final static String VERSION_STRING = \"$(VERSION)\";" >> $@
	echo "}" >> $@

$(NAME).tar: *.java *.php *.js *.css Makefile lib/*.jar LICENSE limewirechanges.zip README
	tar cvf $@ $^

$(NAME).jar: all manifest.mf *.properties LICENSE limewirechanges.zip Makefile
	for f in lib/*.jar; do \
	  jar xvf $$f; \
  done
	jar cvfm $@ manifest.mf *.class org *.properties LICENSE
	mkdir -p release
	cp $@ release/$(DIST).jar
	cp release/$(DIST).jar ~/Desktop

svn: *.php *.css Makefile *.js *.java
	svn add $^

api:
	mkdir -p api

doc: api all
	javadoc -classpath `cat .classpath` -windowtitle "$(NAME) API" -d api *.java

manifest.mf: Makefile
	echo "Main-class: $(MAIN)" > $@

allclean: clean distclean mfclean jarclean genclean docclean

jarclean:
	rm -rf $(NAME).jar META-INF org release/$(DIST).jar

mfclean:
	rm -f manifest.mf

distclean:
	rm -f $(NAME).tar $(NAME).tar.gz release/$(DIST).tar release/$(DIST).tar.gz

genclean:
	rm -f $(GENFILES)

docclean:
	rm -rf api

clean:
	rm -f *~ *.class .classpath